package com.feihong.yw189.utils;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.text.format.Time;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.feihong.yw189.R;
import com.feihong.yw189.bluetooth.utils.util.ToastUtils;
import com.feihong.yw189.paidan.inter.BeiZhuListener;

import cn.qqtheme.framework.picker.DatePicker;

public class SelectDataUtils {
    private static String data = "";
    private static String mYear = "";
    private static String mMonth = "";
    private static String mDay = "";

    public static void showBeiZhuDialog(Activity context, BeiZhuListener mTuiDanListListener) {

        final DatePicker picker = new DatePicker(context);
        //获取当前时间
        Time time = new Time();
        time.setToNow();
        int year = time.year;
        int month = time.month;
        int monthDay = time.monthDay;

        mYear = year + "";
        mMonth = month + 1 + "";
        mDay = monthDay + "";

        View headerView = View.inflate(context, R.layout.beizhu_head, null);
        View footerView = View.inflate(context, R.layout.beizhu_foot, null);
        EditText tv_tdly = headerView.findViewById(R.id.tv_tdly);

        TextView td_ok = footerView.findViewById(R.id.td_ok);
        TextView td_cancle = footerView.findViewById(R.id.td_cancle);

        picker.setHeaderView(headerView);
        picker.setFooterView(footerView);
        picker.setUseWeight(true);

        picker.setCanceledOnTouchOutside(false);
        picker.setGravity(Gravity.CENTER);//弹框居中
        picker.setContentPadding(8, 0);
        picker.setDividerVisible(false);
        picker.setOffset(2); // 设置显示几条
        picker.setRangeEnd(2110, 4, 25);
        picker.setRangeStart(2010, 4, 25);
        picker.setSelectedItem(year, month + 1, monthDay);
        picker.setResetWhileWheel(false);
        picker.setOnDatePickListener(new DatePicker.OnYearMonthDayPickListener() {
            @Override
            public void onDatePicked(String year, String month, String day) {
                Log.e("onDatePicked", year + "-" + month + "-" + day);
                data = year + "-" + month + "-" + day;
            }
        });

        picker.setOnWheelListener(new DatePicker.OnWheelListener() {
            @Override
            public void onYearWheeled(int index, String year) {
                Log.e("onDatePicked", "------year------" + year);
                mYear = year;
                picker.setTitleText(year + "-" + picker.getSelectedMonth() + "-" + picker.getSelectedDay());
            }

            @Override
            public void onMonthWheeled(int index, String month) {
                Log.e("onDatePicked", "------month------" + month);
                mMonth = month;
                picker.setTitleText(picker.getSelectedYear() + "-" + month + "-" + picker.getSelectedDay());
            }

            @Override
            public void onDayWheeled(int index, String day) {
                Log.e("onDatePicked", "------day------" + day);
                mDay = day;
                picker.setTitleText(picker.getSelectedYear() + "-" + picker.getSelectedMonth() + "-" + day);
            }
        });
        picker.show();

        td_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 转派,备注,取消,预定
                closeInputMethod(td_ok, context);
                String trim = tv_tdly.getText().toString().trim();
                // 备注不能为空
                if (!TextUtils.isEmpty(trim)) {
                    data = mYear + "-" + mMonth + "-" + mDay;
                    // 传递备注和日期值
                    Log.e("onDatePicked", trim + "----" + data);
                    mTuiDanListListener.onBeiZhuBack(trim, data);
                    picker.dismiss();

                } else {
                    ToastUtils.showShortToast(context, "必须填写备注");
                }
            }
        });

        td_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                closeInputMethod(td_cancle, context);
                picker.dismiss();
            }
        });
    }

    private static void closeInputMethod(View view, Activity context) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        boolean isOpen = imm.isActive();
        if (isOpen) {
            // imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);//没有显示则显示
            imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

}
